#include "EventManager.h"


EventManager::EventManager()
{
}

void EventManager::add(Event e)
{
	int i = 0;
	for(i = 0; i < events.size(); i++)
	{
		if(e.getID() == events.get(i).getID())
			break;
	}
	if(i != events.size())
	{
		//add the listener pointer with the same index.
		eventStack.push_back(listeners.get(i));
	}
}

void EventManager::runEvent()
{
	if(eventStack.size() > 0)
	{
		Listener l = eventStack.pop_back();
		l.runDrivers();
	}
}

void EventManager::registerEvent(unsigned int eventCode)
{
	Event e = Event();
	e.setEventCode(eventCode);
	events.push_back(e);
	Listener listener = Listener(e);
	listeners.push_back(listener);
}

void EventManager::registerCallback(Event& e, Driver& d)
{
	int i = 0;
	for(i = 0; i < events.size(); i++)
	{
		if(e.getID() == events.get(i).getID())
			break;
	}
	if(i != events.size())
	{
		Listener l = listeners.get(i);
		l.registerCallback(d);
		listeners.set(i, l);
	}
}

